﻿@using System.Web.Mvc.Html
@using Jb.Framework.Common.Forms;
@using Jb.Framework.Common.Forms.Validation
@using Jb.Framework.Web.Mvc
@model Jb.Framework.Common.Forms.JbTextArea

@{

    var textBoxHtmlAttr = new Dictionary<string, object>();
    InputHelper.InputAccessible(Model, (VisibilityMode)ViewBag.VisibleMode, ref textBoxHtmlAttr);
    textBoxHtmlAttr.Add("class", "input-block-level"); // @Salman please add your style here 
    textBoxHtmlAttr.Add("Rows", 2);
    textBoxHtmlAttr.Add("cols", 20);
    textBoxHtmlAttr.Add("style", "resize: vertical;");

    textBoxHtmlAttr.Add("placeholder", Model.PlaceHolder);

    if (Model.Validations.Any(x => x.Type != ElementValidationType.None))
    {
        textBoxHtmlAttr.Add("data-val", "true");
    }
    if (Model.Validations.Any(x => x.Type == ElementValidationType.Required))
    {
        textBoxHtmlAttr.Add("data-Val-Required", Model.Validations.First(x => x.Type == ElementValidationType.Required).Message);
    }

    var titlevalue = Model.Title;

    // if field is required add star
    if (Model.Validations.Any(t => t.Type == ElementValidationType.Required))
    {
        titlevalue += " <span class='star'>*</span>";
    }
    var labelHtmlAttr = new Dictionary<string, object> { { "class", "Custom-label" }, { "data-value", titlevalue } };
    
}

@*<div class="pull-left inline-form">*@
@Html.Hidden("ModelType", Model.GetType())

@for (var i = 0; i < Model.Validations.Count; i++)
{
    //Custom model binder require full model name , i add Validations[i] at the end of string that mvc generate
    var hiddnModelBindername = "Validations[" + i + "].QVModelType"; 
    @Html.Hidden(hiddnModelBindername, Model.Validations[i].GetType())
        
    @Html.HiddenFor(x => x.Validations[i].IsChecked)
    @Html.HiddenFor(x => x.Validations[i].Type)
    @Html.HiddenFor(x => x.Validations[i].Message)

    if (Model.Validations[i] is ElementMaxLengthValidation)
    {

        // these properties is in derived class and for correct binding we nedd this
        var validations = Model.Validations[i] as ElementMaxLengthValidation;
        var hiddenMinModelName = "Validations[" + i + "].Length";
    @Html.Hidden(hiddenMinModelName, validations.Length)
    }

}


<div class="pull-left form-group" style="display: @(Model.VisibleMode != VisibilityMode.Both && Model.VisibleMode != (VisibilityMode)ViewBag.VisibleMode ? "none" : "block")">
    <!--To Bind Label to its textbox -->
    @Html.LabelFor(x => x.Value, labelHtmlAttr)

    @Html.GenerateHiddenFields(Model)

    @foreach (var accRole in Model.AccessRole)
    { 
        @Html.HiddenFor(modelItem => accRole)
    }

    @Html.TextAreaFor(x => x.Value, htmlAttributes: textBoxHtmlAttr)
    @if (!string.IsNullOrEmpty(Model.HelpText))
    {
        <a class="name-info" data-container="body" rel="popover" data-placement="top" data-content="@Model.HelpText"></a>
    }
    @Html.ValidationMessageFor(model => model.Value)


</div>




<script type="text/javascript">

    //get data-value and set to html to correct show value
    $(".Custom-label").each(function () {
        // alert($(this).data("value"));
        $(this).html($(this).data("value"));
    });

</script>



